<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js" type="text/javascript"></script>
</head>
<body>
    <div id="app">
        <!-- @increment, @decrement监听点击事件 -->
        <child-cpn @increment-emit="changeTotal" @decrement-emit="changeTotal"></child-cpn>
        <h2>当前计数：{{total}}</h2>
    </div>
    <!-- 组件模板 -->
    <template id="childCpn">
        <div>
            <!--  @click监听点击事件，调用的是方法-->
            <button @click="increment">+1</button>
            <button @click="decrement">-1</button>
        </div>
    </template>

    <script>
        // 一个vue实例
        var app = new Vue({
            el: "#app",
            data: {
                total: 0
            },
            methods: {
                changeTotal(counter){
                    this.total = counter;
                    // console.log(this.total);
                }
            },
            // 子组件
            components:{ 
                'child-cpn': {
                template: "#childCpn",
                data: function(){
                    return{
                        counter: 0
                    }
                },
                methods: { // 自定义事件
                    increment: function(){
                        this.counter++;
                        this.$emit('increment-emit', this.counter); // increment事件触发后，自动触发increment-emit事件
                    },
                    decrement: function(){
                        this.counter--;
                        this.$emit('decrement-emit', this.counter);
                    }
                }
                }
            }
        })
    </script>
</body>
</html>